<template>
  <!-- 44装备组件-->
  <component
    wd="900"
    :is="new_model"
    v-model:window_open="val.md_eq"
    :title="`44装备查询`"
  >
    <template #content>
      <component :is="comp.Pvp44eq" :ct="ct" :val="val"></component>
    </template>
  </component>

  <!-- 44摆位组件-->

  <component
    wd="900"
    :is="new_model"
    v-model:window_open="val.md_pos"
    :title="`作业站位查询`"
  >
    <template #content>
      <component :is="comp.Pvp44pos" :ct="ct" :val="val"></component>
    </template>
  </component>

  <component
    wd="900"
    :is="new_model"
    v-model:window_open="val.md_upwork"
    :title="`圆形作业投稿上传`"
  >
    <template #content>
      <component :is="comp.Pvp44up" :ct="ct" :val="val"></component>
    </template>
  </component>

  <!-- 一图流组件 -->
  <component
    :is="comp.tomddr"
    :text="val.text"
    :comp="comp.admin_group"
    :ct="ct"
    :val="val"
    mdname="md_review"
  />
  <!-- 攻略组组件 -->
  <component
    :is="comp.tomddr"
    :text="val.text"
    :comp="comp.admin_group"
    :ct="ct"
    :val="val"
    mdname="md_review"
  />

  <div class="dljjc_box3_x">
    <template v-if="$route.query.imgsv != 1">
      <!-- 分类：搜索排名-->
      <template v-if="val.type == 'rank'">
        <!-- 搜索排名组件 all/pvp44_rank.vue -->
        <component :is="comp.pvp44_rank" :val="val" :ct="ct" :cfg="cfg" />
      </template>

      <!-- 分类：首页 -->
      <template v-if="val.type == 'index'">
        <!-- 首页组件 all/pvp44_index.vue -->
        <component
          :is="comp.pvp44_index"
          :comp="comp"
          :val="val"
          :ct="ct"
          :cfg="cfg"
          :route="route"
        />
      </template>
    </template>
  </div>

  <div
    v-if="route.query.imgsv == 1"
    style="
      position: fixed;
      bottom: 0;
      background-color: rgb(0 0 0 / 20%);
      width: 100%;
      left: 0;
      padding-top: 10px;
      padding-bottom: 10px;
      text-align: center;
      text-shadow: 0 0 3px #000;
    "
  >
    第 {{ val.page }} 页 / 共 {{ val.maxpage }} 页 （点击 BOT 按钮切换分页）
  </div>
</template>

<script>
import Pvp44Rank from "./all/pvp44_rank.vue";
import Pvp44Index from "./all/pvp44_index.vue";
import Pvp44eq from "./all/pvp_work_eq.vue";
import new_model from "@/comp/new_modal.vue";
import Pvp44pos from "./all/pvp_work_pos.vue";
import Pvp44up from "./all/up_pvp_work.vue";
import { getrole, strtorole } from "@/cfg/function.js";
import { useRoute, useRouter } from "vue-router";
import { cfg } from "@/cfg/cfg.js";
import img_atk from "@/assets/img/战士.png";
import img_def from "@/assets/img/坦克.png";
import img_cap from "@/assets/img/cap01.png";
import img_defcap from "@/assets/img/cap02.png";
import { defineAsyncComponent } from "vue";
import img_fire from "@/assets/img/火属性.png";
import img_water from "@/assets/img/水属性.png";
import img_land from "@/assets/img/土属性.png";
import img_light from "@/assets/img/光属性.png";
import img_dark from "@/assets/img/暗属性.png";
import img_void from "@/assets/img/虚属性.png";

export default defineComponent({
  setup() {
    const route = useRoute();
    if (window.myFunction == null) {
      window.myFunction = function () {
        return false;
      };
    }
    const comp = {
      Pvp44up,
      Pvp44pos,
      Pvp44eq,
      upwork: {
        comp: Pvp44up,
        mdname: "md_upwork",
        text: {
          title: "竞技场作业上传",
        },
      },
      pos: {
        comp: Pvp44pos,
        mdname: "md_pos",
        text: {
          title: "竞技场摆位查询",
        },
      },
      eq: {
        comp: Pvp44eq,
        mdname: "md_eq",
        text: {
          title: "竞技场角色装备查询",
        },
      },
      admin_group: defineAsyncComponent(() => import("./all/admin_group.vue")),
      tomddr: defineAsyncComponent(() => import("@/views/gt/comp/tongyong/to_mddr.vue")),
      pvp44_rank: Pvp44Rank,
      pvp44_index: Pvp44Index,
    };

    let val = reactive({
      cktab: "chap1",
      img: {
        火: img_fire,
        水: img_water,
        土: img_land,
        光: img_light,
        暗: img_dark,
        虚: img_void,
      },
      v_editdata_atk: [],
      capmode: true,
      maxcount: 0,
      maxpage: 0,
      type: "index",
      page: 0,
      v_editdata: [],
      md_upwork: false,
      soname: "",
      ss: ["火", "水", "土", "光", "暗", "虚"],
      team: [],
      team_icon: [],
      cache_team_icon: [],
      worklist: [],
      so: false,
      starban: [1, 2],
      cyteam: [
        {
          name: "cd刀",
          icon: [
            "invader_knight.png",
            "demon_engineer.png",
            "robot_tanker.png",
            "witch_coco.png",
          ],
        },
        {
          name: "臀投",
          icon: [
            "vampire_captain.png",
            "witch_coco.png",
            "hero_ai.png",
            "invader_knight.png",
          ],
        },
        {
          name: "姐投",
          icon: [
            "parvati.png",
            "witch_coco.png",
            "vampire_captain.png",
            "invader_knight.png",
          ],
        },
        {
          name: "火云",
          icon: ["demon_powergirl.png", "shuran.png", "fire_harpy.png", "parvati.png"],
        },
        {
          name: "灯光",
          icon: ["future_princess.png", "eleanor.png", "hero_ai.png", "store_angel.png"],
        },
        {
          name: "公灯",
          icon: [
            "future_princess.png",
            "witch_coco.png",
            "invader_knight.png",
            "hero_ai.png",
          ],
        },
        {
          name: "公梅琴刀",
          icon: [
            "future_princess.png",
            "china_hero_boy_ascent.png",
            "invader_knight.png",
            "eleanor.png",
          ],
        },
        {
          name: "治疗土",
          icon: [
            "vampire_noble_ascent.png",
            "tanker_ascent.png",
            "festival_girl.png",
            "parvati.png",
          ],
        },
        {
          name: "cd银河",
          icon: [
            "dokkaebi.png",
            "future_knight.png",
            "idol_captain.png",
            "tanker_ascent.png",
          ],
        },
        {
          name: "土盾三暗",
          icon: [
            "tanker_ascent.png",
            "witch_coco.png",
            "invader_knight.png",
            "vampire_noble_ascent.png",
          ],
        },
        {
          name: "人鱼登投",
          icon: [
            "plague_doctor.png",
            "witch_coco.png",
            "invader_knight.png",
            "mermaid.png",
          ],
        },
        {
          name: "公鱼",
          icon: [
            "future_princess.png",
            "witch_coco.png",
            "invader_knight.png",
            "mermaid.png",
          ],
        },
        {
          name: "吸血鬼暗",
          icon: [
            "vampire_noble_ascent.png",
            "tanker_ascent.png",
            "invader_knight.png",
            "witch_coco.png",
          ],
        },
        {
          name: "远暴吸血鬼(狐狸)",
          icon: [
            "vampire_noble_ascent.png",
            "vampire_lord.png",
            "eight_tail.png",
            "fallen_queen.png",
          ],
        },
        {
          name: "远暴暗奶(土姐)",
          icon: [
            "vampire_noble_ascent.png",
            "vampire_lord.png",
            "parvati.png",
            "fallen_queen.png",
          ],
        },
        {
          name: "盾三奶",
          icon: ["tanker_ascent.png", "festival_girl.png", "watcher.png", "mermaid.png"],
        },
      ],

      text: {
        title: "坎公区攻略组",
      },
      md_review: false,
      md_ytl: true,
      text: {
        title: "44防守进攻一图流",
      },
    });

    let ct = reactive({
      swcapmode: () => {
        if (val.capmode == true) {
          val.capmode = false;
        } else {
          val.capmode = true;
        }
      },
      //查询摆位
      get_ps: (item) => {
        val.posid = item.md5;
        val.postm = item;
        val.md_pos = true;
      },
      //查询装备
      get_eq: (item) => {
        val.posid = item.md5;
        val.postm = item;
        val.md_eq = true;
      },
      //清空角色选择
      re_role: () => {
        val.team = [];
        val.team_icon = [];
      },
      //上传作业
      uppvpwork: () => {
        //如果队伍为空 则默认生成4个null
        if (val.team_icon?.length <= 0 || val.team_icon == null) {
          val.v_editdata = [null, null, null, null];
        } else {
          //浅拷贝
          val.v_editdata = val.team_icon.slice();
          if (val.v_editdata[1] == null) {
            val.v_editdata.push([]);
          }
          if (val.v_editdata[2] == null) {
            val.v_editdata.push([]);
          }
          if (val.v_editdata[3] == null) {
            val.v_editdata.push([]);
          }
        }
        val.v_editdata_atk = [null, null, null, null];
        val.md_upwork = true;
      },
      //更新数组
      upteam: () => {
        val.team_icon = [];
        val.team.forEach((element) => {
          val.team_icon.push(element.icon);
        });
      },
      //搜索作业
      search_work: async () => {
        val.so = false;

        // 检查数组角色数量
        if (val.team_icon.length <= 0) {
          cfg.msg.error(`未勾选任何角色`, { duration: 2e3 });
          return "error";
        }

        if (route.query.page >= 1) {
          val.page = route.query.page;
        } else {
          val.page = 1;
        }

        try {
          const res = await cfg.axios.get(`${cfg.url}`, {
            params: {
              t: "get/work/get_pvpwork",
              role: val.team_icon,
              capmode: val.capmode,
              page: val.page,
            },
          });

          val.cache_team_icon = val.team_icon.slice();
          val.so = true;
          const data = res.data.data;
          val.maxcount = data.count;
          val.maxpage = data.maxpage;

          if (data.count <= 0) {
            val.worklist = [];
            cfg.msg.error(`搜不到相似的作业，请尝试上传作业。`, { duration: 2e3 });
          } else {
            val.worklist = data.data;
          }
          val.cktab = "chap3";
          return "ok";
        } catch (err) {
          val.so = false;
          cfg.msg.error(`未知错误：${err.message}`, { duration: 2e3 });
          return "error";
        }
      },
      search_work_add: async () => {
        if (val.page >= val.maxpage) {
          cfg.msg.error("没有更多了", { duration: 2e3 });
          return false;
        }
        val.page++;
        const res = await cfg.axios.get(`${cfg.url}`, {
          params: {
            t: "get/work/get_pvpwork",
            role: val.cache_team_icon,
            capmode: val.capmode,
            page: val.page,
          },
        });

        val.so = true;

        const data = res.data.data;
        val.maxcount = data.count;
        val.maxpage = data.maxpage;
        //val.worklist合并新data
        val.worklist = val.worklist.concat(data.data);
      },
      //设置队长
      exc_role: (key) => {
        // 交换两个元素的位置
        const data = val.team[0];
        const data2 = val.team[key];
        val.team[0] = data2;
        val.team[key] = data;
        ct.upteam();
      },
      //选择角色
      click_role: (role) => {
        // 检查 角色的icon 是否在数组中 存在则删除角色
        if (val.team.some((item) => item.icon == role.icon)) {
          val.team = val.team.filter((item) => item.icon != role.icon);
          ct.upteam();
          return false;
        }
        // 检查数组角色数量是否超出4个
        if (val.team.length >= 4) {
          cfg.msg.error(`最多选择四个角色`, { duration: 2e3 });
          return false;
        }
        //否则添加到数组中
        val.team.push(role);
        ct.upteam();
      },
      //bot搜索作业
      bot_sozy: async (name) => {
        let arr;
        const new_name = strtorole(name);
        if (new_name == false) {
          return false;
        }
        arr = {
          code: 0,
          type: "msg",
          msg: "找到了角色：(" + new_name + ")搜索作业中",
        };

        window.myFunction(arr);
        val.team_icon = new_name;
        const back = await ct.search_work();
        if (back == "ok") {
          arr = {
            code: 0,
            type: "jt_send_msg",
            msg: "截图保存中",
          };
        } else {
          arr = {
            code: 0,
            type: "send_msg",
            msg: "发生错误：" + back,
          };
        }
        window.myFunction(arr);
        return;
      },
    });

    //点赞
    ct.gj_dw = async (type, posid) => {
      if (type == "gj") {
        if (posid.gj == null) {
          posid.gj = 0;
        }
        posid.gj++;
      } else {
        if (posid.dw == null) {
          posid.dw = 0;
        }
        posid.dw++;
      }
      const res = await cfg.axios.get(`${cfg.url}`, {
        params: {
          t: "get/work/pvpwork_gj",
          posid: posid["_id"]["$oid"],
          type: type,
        },
      });
      const data = res.data;
      cfg.msg.info(data.msg, { duration: 2e3 });
      if (data.code == -1) {
        if (type == "gj") {
          posid.gj--;
        } else {
          posid.dw--;
        }
      }
    };

    onMounted(async () => {
      await getrole(true, null, "order_44_ban2");

      if (route.query.name != null) {
        ct.bot_sozy(route.query.name);
      }
    });

    return {
      comp,
      val,
      ct,
      cfg,
      route,
      img_atk,
      img_def,
      img_cap,
      img_defcap,
      new_model,
    };
  },
  async mounted() {
    //strtorole('克雷格x');
  },

  watch: {
    "$route.query.rd": {
      handler(newVal) {
        if (this.route.query.type2 == "pvp44") {
          this.ct.bot_sozy(this.route.query.name);
        } else {
          return;
        }
      },
    },
  },
});
</script>
